package com.kaizeli.website.controller;

import com.kaizeli.website.pojo.dto.base.PageDTO;
import com.kaizeli.website.pojo.entity.CompanyDO;
import com.kaizeli.website.pojo.vo.CompanyVO;
import com.kaizeli.website.query.CompanyQuery;
import com.kaizeli.website.response.Result;
import com.kaizeli.website.service.CompanyService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 公司管理控制器
 */
@Slf4j
@RestController
@RequestMapping("/api/companies")
@RequiredArgsConstructor
@Tag(name = "公司管理", description = "公司信息管理相关接口")
@SecurityRequirement(name = "Bearer Authentication")
public class CompanyController {
    
    private final CompanyService companyService;
    
    @Operation(summary = "分页查询公司列表", description = "支持按公司名称、编码、状态等条件分页查询公司信息")
    @GetMapping("/page")
    public Result<PageDTO<CompanyVO>> pageQuery(CompanyQuery query) {
        log.info("分页查询公司列表，查询条件：{}", query);
        PageDTO<CompanyVO> result = companyService.pageQuery(query);
        return Result.success(result);
    }
    
    @Operation(summary = "根据ID查询公司详情", description = "根据公司ID查询公司详细信息")
    @GetMapping("/{id}")
    public Result<CompanyVO> getById(
            @Parameter(description = "公司ID", required = true, example = "1")
            @PathVariable Integer id) {
        log.info("根据ID查询公司详情，ID：{}", id);
        CompanyVO result = companyService.getById(id);
        return Result.success(result);
    }
    
    @Operation(summary = "根据编码查询公司", description = "根据公司唯一编码查询公司信息")
    @GetMapping("/code/{code}")
    public Result<CompanyVO> getByCode(
            @Parameter(description = "公司编码", required = true, example = "COMP001")
            @PathVariable String code) {
        log.info("根据编码查询公司，编码：{}", code);
        CompanyVO result = companyService.getByCode(code);
        return Result.success(result);
    }
    
    @Operation(summary = "新增公司", description = "创建新的公司信息")
    @PostMapping
    public Result<Integer> save(
            @Parameter(description = "公司信息", required = true)
            @RequestBody CompanyDO companyDO) {
        log.info("新增公司，公司信息：{}", companyDO);
        // 清除ID，确保使用数据库自增ID
        companyDO.setId(null);
        Integer result = companyService.save(companyDO);
        return Result.success(result);
    }
    
    @Operation(summary = "更新公司信息", description = "根据ID更新公司信息")
    @PutMapping("/{id}")
    public Result<Boolean> update(
            @Parameter(description = "公司ID", required = true, example = "1")
            @PathVariable Integer id,
            @Parameter(description = "更新的公司信息", required = true)
            @RequestBody CompanyDO companyDO) {
        log.info("更新公司信息，ID：{}，更新信息：{}", id, companyDO);
        companyDO.setId(id);
        Boolean result = companyService.update(companyDO);
        return Result.success(result);
    }
    
    @Operation(summary = "根据ID删除公司", description = "根据公司ID删除公司信息")
    @DeleteMapping("/{id}")
    public Result<Boolean> deleteById(
            @Parameter(description = "公司ID", required = true, example = "1")
            @PathVariable Integer id) {
        log.info("根据ID删除公司，ID：{}", id);
        Boolean result = companyService.deleteById(id);
        return Result.success(result);
    }
    
    @Operation(summary = "根据状态查询公司列表", description = "根据公司状态查询公司列表")
    @GetMapping("/status/{status}")
    public Result<List<CompanyVO>> listByStatus(
            @Parameter(description = "公司状态", required = true, example = "active")
            @PathVariable String status) {
        log.info("根据状态查询公司列表，状态：{}", status);
        List<CompanyVO> result = companyService.listByStatus(status);
        return Result.success(result);
    }
    
    @Operation(summary = "查询所有公司列表", description = "查询系统中所有公司信息")
    @GetMapping("/all")
    public Result<List<CompanyVO>> listAll() {
        log.info("查询所有公司列表");
        List<CompanyVO> result = companyService.listAll();
        return Result.success(result);
    }
    
    @Operation(summary = "搜索公司", description = "根据名称或编码搜索公司信息")
    @GetMapping("/search")
    public Result<List<CompanyVO>> search(
            @Parameter(description = "搜索关键词（公司名称或编码）", required = true, example = "凯泽利")
            @RequestParam String keyword) {
        log.info("搜索公司，关键词：{}", keyword);
        List<CompanyVO> result = companyService.search(keyword);
        return Result.success(result);
    }
}

